<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Sync
 *
 * Sincronizaci�n con hattrick.org.
 * Toda la sincronizaci�n se hace fuera del entorno de CodeIgniter.
 *
 * @package
 *
 * @author ImHosb
 * @author www.imhosb.com
 *
 * @access public
 */
class Sync extends CI_Controller {

    public $layout = DEFAULTLAYOUT;

    /**
     * Sync::__construct()
     *
     * @return
     */
    function __construct() {
        parent::__construct();

        $this->load->library('sync_sync', get_user_id());
    }

    /**
     * Sync::index()
     *
     * Muestra la vista con el men� de sincronizaci�n.
     *
     * @return
     */
    function index() {
        $synchronization = new Synchronization();
        $where = array(
            'user_id' => get_user_id(),
            'position_queue !=' => 0
        );
        if($synchronization->where($where)->count() == 0) {
            $synchronization->select_max('position_queue')->get();
            $position_queue = $synchronization->position_queue;

            $dateTime = new DateTime();

            unset($synchronization->id);
            $synchronization->user_id = get_user_id();
            $synchronization->start_date = (string) $dateTime->format('Y-m-d H:i:sP');
            $synchronization->end_date = 0;
            $synchronization->position_queue = ++$position_queue;

            db_save_as_new($synchronization);
        }

        $this->load->view('sync');
    }

    function authorize () {
        $this->sync_sync->authorize();
    }

    function save_tokens () {
        $this->sync_sync->save_tokens();
    }

    function get_data() {
        $this->sync_sync->get_data();
    }
}

/* End of file sync.php */
/* Location: ./app/modules/sync/controllers/sync.php */